package smsweb.server;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Sms {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	@Persistent
	private String address;
	@Persistent
	private String message;
	@Persistent
	private Date date;
	@Persistent
	private long id;
	@Persistent
	private boolean read = false;
	@Persistent
	private boolean sent = false;
	@Persistent
	private boolean incomingMessage = false;
	
	public Sms(Key key, String address, String message, Date date, long id,
			boolean read, boolean sent, boolean incomingMessage) {
		super();
		this.key = key;
		this.address = address;
		this.message = message;
		this.date = date;
		this.id = id;
		this.read = read;
		this.sent = sent;
		this.incomingMessage = incomingMessage;
	}	
	
	public String getAddress() {
		return address;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public boolean isRead() {
		return read;
	}

	public void setRead(boolean read) {
		this.read = read;
	}

	public boolean isSent() {
		return sent;
	}

	public void setSent(boolean sent) {
		this.sent = sent;
	}

	public boolean isIncomingMessage() {
		return incomingMessage;
	}

	public void setIncomingMessage(boolean incomingMessage) {
		this.incomingMessage = incomingMessage;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}
	
	public String toNetworkMessage() {
		StringBuffer sb = new StringBuffer();
		sb.append("2");
		sb.append("||");
		sb.append(getId());
		sb.append("||");
		sb.append(getAddress());
		sb.append("||");
		sb.append(getDate().getTime());
		sb.append("||");
		sb.append(getMessage());
		sb.append("|&&|");
		return sb.toString();
	}
	
	public static Sms parseSms(String message){
		int typeIndex = message.indexOf("||");
        String strType = message.substring(0, typeIndex);
        int idIndex = message.indexOf("||", typeIndex + 2);
        String strId = message.substring(typeIndex + 2, idIndex);
        int type = Integer.parseInt(strType);

        int addressIndex = message.indexOf("||", idIndex + 2);
        String address = message.substring(idIndex + 2, addressIndex);
        int dateIndex = message.indexOf("||", addressIndex + 2);
        String strDate = message.substring(addressIndex + 2, dateIndex);
        int messageIndex = message.indexOf("||", addressIndex + 2);
        int endIndex = message.indexOf("|&&|");
       
        String strMessage = message.substring(messageIndex + 2, endIndex);
        long id = Long.parseLong(strId);
        long lDate = Long.parseLong(strDate);
        Key key = KeyFactory.createKey(Sms.class.getSimpleName(), id);
        
        Sms retVal = new Sms(key, address, strMessage, new Date(lDate), id, false, false, true);
        return retVal;
	}

	public Sms() {
		super();
	}


}
